﻿using System;
using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class _53info : Information
    {
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("submit", "1");
            pd.Add("username", name);
            pd.Add("password", pass);
            pd.Add("x", "31");
            pd.Add("y", "16");
            string html = source.Post("http://www.53info.com/member/login.php", pd.Read(), "http://www.53info.com");
            if (html.IndexOf("class=\"user_wel\">您好，欢迎回来</div>") != -1)
            {
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult result = new InformationResult();
            ParametersData pd = new ParametersData();
            pd.Add("action", "add");
            pd.Add("mid", "5");
            pd.Add("itemid", "0");
            pd.Add("forward", "http://www.53info.com/member/my.php");
            pd.Add("post[typeid]", "0");
            pd.Add("post[title]", title);
            pd.Add("post[catid]", site.TypeId[0]);
            pd.Add("post[model]", pif.Get("产品名称"));
            pd.Add("post[standard]", pif.Get("产品规格"));
            pd.Add("post[brand]", pif.Get("产品名称"));
            pd.Add("post[content]", content);
            pd.Add("post[thumb]", UpImage());
            pd.Add("post[totime]", "");
            pd.Add("post[unit]", pif.Get("单位"));
            pd.Add("post[price]", pif.Get("价格"));
            pd.Add("post[minamount]", "1");
            pd.Add("post[amount]", pif.Get("库存数量"));
            pd.Add("post[days]", "2");
            pd.Add("post[mycatid]", "0");
            pd.Add("post[elite]", "0");
            pd.Add("answer", GetVcode());
            pd.Add("captcha", vcode);
            pd.Add("submit", " 提 交 ");
            string html = source.Post("http://www.53info.com/member/my.php", pd.Read(), "http://www.53info.com/member/my.php?mid=5&action=add");
            if (html.IndexOf("<script type=\"text/javascript\">alert('") != -1)
            {
                if (html.IndexOf("24小时内最多发布5条") != -1)
                {
                    result.Code = 3;
                    result.Content = "24小时内最多发布5条";
                    return result;
                }
                else
                {
                    result.Content = Regex.Match(html, "text/javascript\">alert('([^']+)").Groups[1].Value;
                }
            }
            html = source.Get("http://www.53info.com/member/my.php?mid=5&status=2", "http://www.53info.com/member/my.php");
            if (Regex.IsMatch(html, title))
            {
                result.Code = 1;
                result.Content = "http://www.53info.com/sell/show-" + Regex.Match(html, "itemid=([0-9]+)\" class=\"t\">" + title).Groups[1].Value + ".html";
            }
            return result;
        }

        public string UpImage()
        {
            string html = "";
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("fid", "");
            sfd.Write("moduleid", "5");
            sfd.Write("from", "album");
            sfd.Write("old", "");
            sfd.Write("isremote", "0");
            sfd.Write("remote", "http://");
            //发送文件
            sfd.WriteFile("upalbum", RandomPicture.Get());
            //尾部数据
            sfd.Write("width", "100");
            sfd.Write("height", "100");
            //结尾
            html = source.Post("http://www.53info.com/upload.php", sfd.GetContentType(), sfd.Read(), "http://www.53info.com/member/my.php?mid=5&action=add");
            return Regex.Match(html, "getAlbum\\(\"([^\"]+)\"").Groups[1].Value;
        }

        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://www.53info.com/api/captcha.png.php?action=image", "http://www.53info.com/member/my.php?mid=5&action=add");
        }
        //获取验证码
        private string GetVcode()
        {
            string html = source.Get("http://www.53info.com/api/captcha.png.php?action=question&refresh=0.6232850684158494.js&_utm_source=203efa", "http://www.53info.com/member/my.php?mid=5&action=add");
            if (html.IndexOf("本站名称") != -1)
            {
                return "网商之窗";
            }
            int a = Convert.ToInt32(Regex.Match(html, "HTML = \"([0-9]+)").Groups[1].Value);
            int b = Convert.ToInt32(Regex.Match(html, "HTML = \"[0-9]+.([0-9]+)=").Groups[1].Value);
            string fuhao = Regex.Match(html, "HTML = \"[0-9]+(.)").Groups[1].Value;
            switch (fuhao)
            {
                case "+":
                    {
                        return (a + b).ToString();
                    }
                case "-":
                    {
                        return (a - b).ToString();
                    }
                case "*":
                    {
                        return (a * b).ToString();
                    }
            }
            return "";
        }
    }
}
